package triFileSystem;

import transaction.*;
import triStorage.IRandIntAccess;
import triStorage.TriMMPFile;

public class RandPageAccess implements IRandIntAccess {

	TriMMPFile mmpfile;
	long PAGE_SIZE;
	
	public RandPageAccess(TriMMPFile mmpfile) {
		this.mmpfile = mmpfile;
		this.PAGE_SIZE = mmpfile.PAGE_SIZE;
	}
	
	@Override
	public void writeInt(long offset, int value, Transaction tx) {
		mmpfile.writeInt((int)(offset /PAGE_SIZE), (int)(offset % PAGE_SIZE), value, tx);
	}
	
	@Override
	public Integer readInt(long offset) {
		return mmpfile.readInt((int)(offset / PAGE_SIZE), (int)(offset % PAGE_SIZE));
	}

	/*
	@Override
	//never used. doesn't work
	public String readString(long offset) {
		int len = readInt(offset);
		
		if (len < 0)
			return null;
		else if (len == 0)
			return "";
		
		offset += Constants.INT_SIZE;
		
		byte[] buffer = new byte[len];
		
		mmpfile.readBytes((int)(offset / PAGE_SIZE), (int)(offset % PAGE_SIZE), buffer, 0, len);
		return Converter.Bytes2String(buffer, 0);
	}

	@Override
	//never used. doesn't work
	public void writeString(long offset, String value) {
		if (value == null) { 
			writeInt(offset, -1);
			return;
		}
		
		int len = value.length();
		writeInt(offset, len);
		if (len == 0)
			return;
		
		byte[] buffer = new byte[len];
		Converter.String2Bytes(value, buffer, 0);
		mmpfile.writeBytes((int)(offset / PAGE_SIZE), (int)(offset % PAGE_SIZE), buffer, 0, len);
	}
	 */
	
	@Override
	public void dispose() {
		//do nothing
	}
}
